package com.itheima.stock.service.imp;

import com.itheima.stock.entity.StockBlockRtInfo;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.entity.StockMarketIndexInfo;
import com.itheima.stock.entity.StockMarketLogPrice;
import com.itheima.stock.mapper.StockBlockRtInfoMapper;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.properties.MarketProperties;
import com.itheima.stock.service.StockService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.vo.InnerMarketVO;
import com.itheima.stock.vo.SectorAllVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    private final StockBlockRtInfoMapper stockBlockRtInfoMapper;
    private final StockBusinessMapper stockBusinessMapper;
    private final StockMarketLogPriceMapper stockMarketLogPriceMapper;
    private final StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    private final MarketProperties marketProperties;

    public StockServiceImpl(StockBusinessMapper stockBusinessMapper, MarketProperties marketProperties, StockMarketIndexInfoMapper stockMarketIndexInfoMapper, StockMarketLogPriceMapper stockMarketLogPriceMapper, StockBlockRtInfoMapper stockBlockRtInfoMapper) {
        this.stockBusinessMapper = stockBusinessMapper;
        this.marketProperties = marketProperties;
        this.stockMarketIndexInfoMapper = stockMarketIndexInfoMapper;
        this.stockMarketLogPriceMapper = stockMarketLogPriceMapper;
        this.stockBlockRtInfoMapper = stockBlockRtInfoMapper;
    }


    @Override
    public List<StockBusiness> getStockBusiness() {
        return stockBusinessMapper.selectAll();
    }

    @Override
    public List<InnerMarketVO> innerIndexAll() {
        // 1. 获取国内大盘指数编码集合
        List<String> ids = marketProperties.getInner();
        // 2. 获取最近的有效的股票交易时间
        LocalDateTime last = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
        // mock data
        String mockDate = "20211226105600";
        last = LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        // 3. 查询
        // 3.1 根据大盘编码和日期查询 股票大盘数据详情表
        List<StockMarketIndexInfo> marketIndexInfoList = stockMarketIndexInfoMapper.selectByIdsAndDateTime(ids, last);
        // 3.2 根据大盘编码和日期查询 股票大盘 开盘价与前收盘价流水表
        LocalDate localDate = last.toLocalDate();
        List<StockMarketLogPrice> marketLogPriceList = stockMarketLogPriceMapper.selectByIdsAndDateTime(ids, localDate);
        //4. 组装数据
        return marketIndexInfoList.stream().map((marketIndexInfo) -> {
                    InnerMarketVO vo = InnerMarketVO.builder()
                            .code(marketIndexInfo.getMarkId())
                            .name(marketIndexInfo.getMarkName())
                            .curDate(marketIndexInfo.getCurTime().toString())
                            .tradeAmt(marketIndexInfo.getTradeAccount())
                            .tradeVol(marketIndexInfo.getTradeVolume())
                            .tradePrice(marketIndexInfo.getCurrentPrice())
                            .upDown(marketIndexInfo.getUpdownRate())
                            .build();
                    marketLogPriceList.forEach((marketLogPrice) -> {
                        if (marketLogPrice.getMarketCode().equals(marketIndexInfo.getMarkId())) {
                            vo.setOpenPrice(marketLogPrice.getOpenPrice());
                            vo.setPreClosePrice(marketLogPrice.getPreClosePrice());
                        } else {
                            vo.setOpenPrice(BigDecimal.ZERO);
                            vo.setPreClosePrice(BigDecimal.ZERO);
                        }
                    });
                    return vo;
                }
        ).toList();
    }

    @Override
    public List<SectorAllVO> sectorAll() {
        LocalDateTime last = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
        // mock data
        String mockDate = "20211221000000";
        last = LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        LocalDateTime begin = DateTimeUtil.getOpenDate(last);
        LocalDateTime end = DateTimeUtil.getCloseDate(last);
        List<StockBlockRtInfo> stockBlockRtInfosList = stockBlockRtInfoMapper.selectByDate(begin,end);
        return stockBlockRtInfosList.stream().map(stockBlockRtInfo -> SectorAllVO.builder()
                .name(stockBlockRtInfo.getBlockName())
                .code(stockBlockRtInfo.getLabel())
                .companyNum(stockBlockRtInfo.getCompanyNum())
                .avgPrice(stockBlockRtInfo.getAvgPrice())
                .tradeAmt(stockBlockRtInfo.getTradeAmount())
                .tradeVol(stockBlockRtInfo.getTradeVolume())
                .curDate(stockBlockRtInfo.getCurTime())
                .updownRate(stockBlockRtInfo.getUpdownRate())
                .build()).toList();
    }
}


